import numpy as np
import csv
import random
from datetime import datetime
from time import strftime


'''
Global and Local Empirical Bayes Smoothers with Gamma Model
'''

def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.DictReader(file(fn), dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = float(record[item])
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

def build_data_list_withoutColumnName(inputCSV):
    sKey = []
    fn = inputCSV
    ra = csv.reader(file(fn), dialect="excel")
    i = 0
    inputyear = currentYear - 1995
    for row in ra:
        sKey.append(inputyear) 
        sKey.append(int(fips_dic[str(int(row[0]))]))
        sKey.append(int(fips_dic[str(int(row[1]))]))
        sKey.append(int(row[2]))

    sKey = np.array(sKey)
    sKey.shape = (-1, 4)   
    return sKey


#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/distinct_fips.csv'
    countyInfo = build_data_list(filepath)

    fips_dic = {}
    for item in countyInfo:
        fips_dic[str(int(item[0]))] = int(item[1])

    year = range(1995,2010)
    #year = range(2008,2010)
    region = range(0,5)

    #year = [1995]
    #region = [0]

    filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/outside_state/'

    output = []
    for currentYear in year:
        print currentYear
        for currentRegion in region:
            print currentRegion
            inputCSV = filepath + str(currentYear) +'_in_outside_state__distribute_region_' + str(currentRegion)+ '.csv'
            #print inputCSV
            ra = csv.reader(file(inputCSV), dialect="excel")
            inputyear = int(currentYear - 1995)
            for row in ra:
                #output.append(inputyear)
                #output.append(fips_dic[str(int(row[0]))])
                #output.append(fips_dic[str(int(row[1]))])
                output.append(currentYear)
                output.append(int(row[0]))
                output.append(int(row[1]))
                output.append(int(row[2]))
    output = np.array(output)
    output.shape = (-1, 4)
    #print output

    
    np.savetxt(filepath + 'total2.csv', output, delimiter=',', fmt = '%i')
    